package com.example.controller;

import com.example.entity.*;
import com.example.entity.res.DoctorRes;
import com.example.entity.res.DynamicRes;
import com.example.entity.res.FileMsg;
import com.example.entity.res.ReturnId;
import com.example.interceptor.TokenNotValidation;
import com.example.service.DoctorService;
import com.example.service.DynamicImgService;
import com.example.service.UserService;
import com.example.utils.AlbumUtil;
import com.example.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/doctor")
public class DoctorController {

    @Autowired
    AlbumUtil albumUtil;

    @Autowired
    DoctorService doctorService;

    @Autowired
    DynamicImgService dynamicImgService;

    @Autowired
    UserService userService;

    /**
     * 写入动态图片
     *
     * @param request
     * @return
     */
    @TokenNotValidation
    @PostMapping("/insertDoctorImg")
    public List<ReturnId> insertDoctorImg(HttpServletRequest request) {
        try {
            List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file");
            List<FileMsg> strings = albumUtil.uploadFilesmore(files);
            List<ReturnId> albumIds = new ArrayList<>();
            for (FileMsg string : strings) {
                DynamicImg dynamicImg = new DynamicImg();
                dynamicImg.setCompressPath(string.getPathNameCompress());
                dynamicImg.setPath(string.getPathName());
                dynamicImg.setCreatDate(new Date());
                int insert = dynamicImgService.insert(dynamicImg);
                ReturnId returnId = new ReturnId();
                returnId.setId(insert);
                albumIds.add(returnId);
            }
            return albumIds;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 上传专家认证
     */
    @TokenNotValidation
    @PostMapping("/uploadDoctor")
    public void uploadDoctor(@RequestBody DoctorRes doctorRes) {
        List<Integer> imgIds = doctorRes.getImgId();
        String imgIdFinal = "";
        if (imgIds.size() != 0) {
            for (Integer imgId : imgIds) {
                imgIdFinal = imgIdFinal + imgId + ",";
            }
            imgIdFinal = imgIdFinal.substring(0, imgIdFinal.length() - 1);
        }
        Doctor doctor = new Doctor();

        doctor.setCreatDate(new Date());
        doctor.setCheckPath(imgIdFinal);
        doctor.setLogo(doctorRes.getLogo());
        doctor.setPassword(doctorRes.getPassword());
        doctor.setUserName(doctorRes.getUserName());
        doctor.setPhone(doctorRes.getPhone());
        doctorService.insert(doctor);
    }

    /**
     * 判断是否是专家
     *
     * @param id
     * @return
     */
    @TokenNotValidation
    @GetMapping("/isDoctor/{id}")
    public Boolean isDoctor(@PathVariable Integer id) {
        User user = userService.selectByPrimaryKey(id);
        if (user.getRoleId() == 3) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 判断自己是否是专家
     *
     * @return
     */
    @TokenNotValidation
    @GetMapping("/isDoctorHimself")
    public Boolean isDoctorHimself(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        token = token.substring(7);
        Integer userId = JwtUtil.getUserId(token);
        User user = userService.selectByPrimaryKey(userId);
        if (user.getRoleId() == 3) {
            return true;
        } else {
            return false;
        }
    }
}
